<!DOCTYPE html>
<html lang="en" xml:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>方案信息管理</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<!--数据与分页-->
<div class="decorationScheme_table_div">
    <!--多条件查询-->
    <div id="searchParam">
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <input type="text" id="name" class="layui-input" autocomplete="off" placeholder="">
            </div>
            <div class="layui-input-inline ">
                <button class="layui-btn" id="search">查询</button>
            </div>
        </div>
    </div>
    <!--显示数据的表格-->
    <table class="layui-hide" id="decorationScheme_table" lay-filter="decorationScheme_table"></table>
    <!--分页相关 View-->
    <div id="laypage" class="layui-laypage-btn"></div>
</div>


<!-- 新增编辑方案信息View-->
<div class="panel panel-default operation_decorationScheme" hidden>
    <div class="panel-heading title"></div>
    <div class="layui-card-body">
        <form class="layui-form" action="" lay-filter="deptInfo" style="width: 700px;margin-top: 10px">
            <input name="id" hidden/>
            <div class="layui-form-item">
                <label class="layui-form-label">方案名称</label>
                <div class="layui-input-block">
                    <input type="text" name="schemeName" placeholder="方案名称" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">效果图片</label>
                <div class="layui-input-block">
                    <div class="layui-upload-drag" id="decorationScheme">
                        <i class="layui-icon"></i>
                        <p>点击上传，或将文件拖拽到此处</p>
                        <div id="uploadDemoView">
                            <hr>
                            <div class="layui-carousel carousel" lay-filter="carousel" id="imageViews">
                                <div carousel-item>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">单价</label>
                <div class="layui-input-block">
                    <input type="number" name="unitPrice" placeholder="单价" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存
                    </button>
                    <button class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
                </div>
            </div>
        </form>
    </div>
</div>

</body>
<script src="/layui/layui.js"></script>
<script src="/js/core.util.js"></script>
<!--新增方案信息按钮、批量删除 按钮-->
<script type="text/html" id="toolbar">
    <div class="layui-btn-group">
        <!--shiro:hasPermission="sys:decorationScheme:delete"-->
        <button type="button" class="layui-btn" lay-event="batchDeleted">
            <i class="layui-icon">&#xe608;</i> 批量删除
        </button>
    </div>
</script>
<!--给方案信息赋予角色按钮-->
<!--shiro:hasPermission="sys:decorationScheme:role:update"  shiro:hasPermission="sys:decorationScheme:update" shiro:hasPermission="sys:decorationScheme:delete" -->
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-xs" lay-event="show">查看方案信息</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>

    layui.use(['jquery', 'form', 'table', 'laypage', 'layer', "upload", "carousel"], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        var laypage = layui.laypage
        var layer = layui.layer;
        var upload = layui.upload;
        var carousel = layui.carousel;

        let decorationSchemePictureList = []

        /*分页查询条件*/
        let searchParam = {
            current: 1,
            size: 10,
            /*以下为多条件查询参数*/
            schemeName: null,
        }

        //改变下时间间隔、动画类型、高度
        let render = carousel.render({
            elem: '#imageViews'
            , interval: 2000
            , anim: 'fade'
            , width: '196px'
            , height: '130px'
            , autoplay: true
            , arrow: 'none'
        });

        carousel.on('trigger(imageViews)', function(obj){ //test1来源于对应HTML容器的 lay-filter="test1" 属性值
            layer.open({
                content: '确定要删这张图片吗么?',
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    $(obj).remove()
                }
            });
        });

        // 关闭父组件通知
        $("#imageViews").click((event)=>{
            event.stopPropagation();
        })

        var adddecorationScheme = function (res) {
            // 图片列表 添加刚上传完的数据
            decorationSchemeList.push({
                src: res.data.src,
                imageName: res.data.title,
                decorationSchemeId: $(".operation_contract input[name=id]").val()
            })
            // html 添加
            $("#imageViews").find("div").append(`
              <div><img src=${"'" + res.data.src + "'"} style="max-width: 196px"></div>
            `)
            // 轮播图重置
            render.reload({
                interval: 2000
                , anim: 'fade'
                , width: '196px'
                , height: '130px'
                , autoplay: true
                , arrow: 'none'
            });
        }

        upload.render({
            elem: '#decorationScheme'
            , url: '/upload'
            , done: function (res) {
                layer.msg('上传成功');
                // 添加html 元素
                adddecorationScheme(res)
            }
        });


        /**
         *  加载分页数据
         */
        function loadPageInfo() {
            CoreUtil.sendAjax("/zx/decorationScheme/page", searchParam, function (res) {
                //初始化分页器
                laypageTable(res.total, searchParam.current);
                //初始化渲染数据
                loadTable(res.records);
                if (res.records != null) {
                    loadTable(res.records);
                }
            }, "GET", false, function (res) {
                //用户列表接口无权限回调
                layer.msg("抱歉！您暂无获取用户列表的权限");
                let noAuthorityData;
                noAuthorityData = [];
                loadTable(noAuthorityData);
            });
        }

        //渲染分页插件
        var laypageTable = function (count, currentPage) {
            laypage.render({
                elem: 'laypage'
                , count: count
                , limit: searchParam.size
                , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                , curr: location.hash.replace('#!currentPage=', '')     //获取起始页
                , hash: 'currentPage'                                   //自定义hash值
                , jump: function (obj, first) {
                    if (!first) {
                        searchParam.current = obj.curr;
                        searchParam.size = obj.limit;
                        loadPageInfo();
                    }
                }
            });
        };

        //渲染table
        var loadTable = function (data) {
            //展示已知数据
            table.render({
                elem: '#decorationScheme_table'
                // 这个 data 为数组数据，直接 放到 data 上即可。
                , data: data
                , even: true
                , limit: data.length
                , limits: [10, 20, 30, 40, 50]
                , toolbar: '#toolbar'
                , cols: [
                    [
                        {type: 'checkbox', fixed: 'left'},
                        {field: 'id', title: 'ID', width: 20, sort: true},
                        {field: 'schemeName', title: '方案名称', width: 230},
                        {
                            field: "unitPrice", title: "单价", width: 500
                        },
                        {width: 300, toolbar: "#tool", title: '操作'}
                    ]
                ]
            });
        };
        loadPageInfo();

        //点击新增事件， 表头工具栏事件
        table.on('toolbar(decorationScheme_table)', function (obj) {
            switch (obj.event) {
                case 'addNewdecorationScheme':
                    $(".title").html("新增方案信息");
                    $(".decorationScheme_table_div").hide();
                    $(".operation_decorationScheme").show();
                    $(".operation_decorationScheme input[name=id]").val("");
                    $(".operation_decorationScheme input[name=schemeName]").val("");
                    // 清理文件上传信息
                    $(".operation_decorationScheme input[name=unitPrice]").val("");
                    $("#imageViews").find("div").html("")
                    form.render(); //更新全部
                    break;
                case 'batchDeleted':
                    let checkStatus = table.checkStatus(obj.config.id);
                    let data = checkStatus.data;
                    if (data.length === 0) {
                        layer.msg("请选择要批量删除的方案信息");
                    } else {
                        let decorationSchemeIds = [];
                        $(data).each(function (index, item) {
                            decorationSchemeIds.push(item.id);
                        });
                        tipDialog(decorationSchemeIds, "选中的");
                    }
                    break;
            }
        });

        //编辑或新增的界面取消事件
        $("#btn_cancel").click(function () {
            //显示表格
            $(".decorationScheme_table_div").show();
            //隐藏编辑窗口
            $(".operation_decorationScheme").hide();
            //可以防止在表单事件操作后自动刷新
            return false;
        });

        //监听提交 新增和编辑  保存按钮
        form.on('submit(submit)', function (data) {
            data.field = {...data.field, decorationSchemePictureList}
            //新增
            if (data.field.id === undefined || data.field.id === null || data.field.id === "") {
                CoreUtil.sendAjax("/zx/decorationScheme", JSON.stringify(data.field), function (res) {
                    //显示表格
                    loadPageInfo();
                    $(".decorationScheme_table_div").show();
                    //隐藏编辑窗口
                    $(".operation_decorationScheme").hide();
                    // 刷新页面，但是不管事儿
                    $(".layui-laypage-btn").click();
                }, "POST", false, function (res) {
                    //新增方案信息无权限提示
                    layer.msg("抱歉！您暂无新增方案信息的权限");
                });
                // 编辑
            } else {
                CoreUtil.sendAjax("/zx/decorationScheme", JSON.stringify(data.field), function (res) {
                    //显示表格
                    $(".decorationScheme_table_div").show();
                    //隐藏编辑窗口
                    $(".operation_decorationScheme").hide();
                    $(".layui-laypage-btn").click();
                }, "POST", false, function (res) {
                    //更新方案信息无权限提示
                    layer.msg("抱歉！您暂无编辑方案信息的权限");
                });
            }
            return false;
        });


        //执行查询
        $("#search").click(function () {
            searchParam.schemeName = $("#name").val();
            searchParam.current = 1;
            loadPageInfo();
        });


        // 操作工具栏 点击赋予角色事件事件
        table.on('tool(decorationScheme_table)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit':
                    $(".decorationScheme_table_div").hide();
                    $(".operation_decorationScheme").show();
                    $(".title").html("编辑方案信息");
                    echoingdecorationScheme(data.id)
                    $(".operation_decorationScheme input[name=id]").val(data.id);
                    $(".operation_decorationScheme input[name=schemeName]").val(data.schemeName);
                    // 清理文件上传信息
                    $(".operation_decorationScheme input[name=unitPrice]").val(data.unitPrice);
                    form.render(); //更新全部
                    break;
                case 'del':
                    var decorationSchemeIds = [];
                    decorationSchemeIds.push(data.id);
                    tipDialog(decorationSchemeIds, "当前");
                    break;
                case 'show':
                    showdecorationScheme(data.id)
                    break;


            }
        });
        //删除前确认对话框
        var tipDialog = function (decorationSchemeIds, name) {
            layer.open({
                content: '确定要删除' + name + "方案信息么?",
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendAjax("/zx/decorationScheme", JSON.stringify(decorationSchemeIds), function (res) {
                        layer.msg(res.message);
                        $(".layui-laypage-btn").click();
                    }, "DELETE", false, function (res) {
                        //删除方案信息无权限
                        layer.msg("抱歉！您暂无删除方案信息的权限");
                    });
                }
            });
        };

        // 加载方案图片信息
        var loaddecorationSchemeList = function (decorationSchemeId, handle) {
            CoreUtil.sendAjax(`/zx/decorationSchemePicture/decorationScheme/${decorationSchemeId}`, null, handle, "GET", false, function (res) {
                //用户列表接口无权限回调
                layer.msg("抱歉！您暂无获取用户列表的权限");
                let noAuthorityData;
                noAuthorityData = [];
                loadTable(noAuthorityData);
            });
        }

        // 显示方案图片信息
        var showdecorationScheme = function (decorationSchemeId) {
            loaddecorationSchemeList(decorationSchemeId, (res) => {
                let list = res.map(decorationScheme=>`<div><img src=${"'" + decorationScheme.src + "'"} style="max-width: 800px"></div>`);
                layer.open({
                    area: ['800px', '600px'],
                    title: "方案展示图片",
                    content: `
                             <div class="layui-carousel carousel" lay-filter="carousel" id="showImage">
                                <div carousel-item>
                                       ${list}
                                </div>
                            </div>
                                `,
                });
                carousel.render({
                    elem: '#showImage'
                    , interval: 2000
                    , anim: 'fade'
                    , width: '800px'
                    , height: '600px'
                    , autoplay: true
                    , arrow: 'none'
                });
            })
        }

        //回显数据
        var echoingdecorationScheme = function (decorationSchemeId) {
            loaddecorationSchemeList(decorationSchemeId, (res) => {
                decorationSchemePictureList = res
                // 清空html 元素
                $("#imageViews").find("div").html("")
                // 添加html 元素
                decorationSchemePictureList.map(decorationScheme => {
                    $("#imageViews").find("div").append(`
                       <div><img src=${"'" + decorationScheme.src + "'"} style="max-width: 196px"></div>
                    `)
                })
                // 轮播图重置
                render.reload({
                    interval: 2000
                    , anim: 'fade'
                    , width: '196px'
                    , height: '130px'
                    , autoplay: true
                    , arrow: 'none'
                });
            })
        }

    });
</script>
</html>

